<?php
/**
 * 工单操作唯一入口
 * User: Administrator
 * Date: 2018/3/31/031
 * Time: 22:28
 */

namespace app\lib\Order\OrderOption;


use app\lib\Order\IsOrder\IsOrder;
use app\lib\Order\OrderExtensionConfig\OrderBindingExtension;
use app\lib\Order\OrderInfo\GetOrderMoney;
use app\lib\Order\OrderInfo\UpdateOrderStatus;
use app\lib\Order\OrderMoney\AddOrderMoneyRecord;
use app\lib\Order\OrderMoney\EditOrderMoneyRecord;
use app\lib\Order\OrderNotice\OrderNotice;
use app\lib\Order\OrderPaymentNotice\AddOrderNotice;
use app\model\OrderInfoModel;

class OrderOption
{
    private $orderId;
    protected $orderInfoModel;
    protected $orderLog;
    protected $data;
    protected $opUid;
    protected $taskOrderId = 0;
    public function __construct($orderId)
    {
        $this->orderId = $orderId;
    }

    public function setOpUid($opUid){
        $this->opUid = $opUid;
        return $this;
    }

    public function setData($data){
        $this->data = $data;
        return $this;
    }

    protected function getOrderInfoModel(){
        if(!isset($this->orderInfoModel)){
            $this->orderInfoModel = new OrderInfoModel();
        }
        return  $this->orderInfoModel;
    }

    protected function getOrderLogModel(){
        if(!isset($this->orderLog)){
            $this->orderLog =  new OrderOptionLog( $this->orderId );
        }
        return   $this->orderLog;
    }
//    /**
//     * 分解任务改变状态
//     */
//    public function addOrderTask(){
//        $data['orderStatus'] = 1;
//        $result = $this->getOrderInfoModel()->where('id',$this->orderId)->update($data);
//        if(!$result){
//            exception('状态修改失败');
//        }
//        $this->writingOrderLog('RESOLVE','任务分解');
//        return true;
//    }

    /**
     * 发布工单改变状态
     * @return bool
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function releaseTask(){
        /*修改工单状态*/
        (new UpdateOrderStatus($this->orderId,$this->opUid))->getCarryOrderStatus()->save();
        /*交款信息实例化*/
        $orderMoneyModel =  (new GetOrderMoney($this->orderId,false));
        /*第一期未交款*/
        if(!($orderMoneyModel->isFirstMoney())){
            $model = new AddOrderNotice();
            /*获取未交款id*/
            $info =  $orderMoneyModel->getNoPayRecord();
            /*生成交款通知记录*/
             $model->setOpUid($this->opUid)->setOrderId($this->orderId)
                ->setContent('发布工单催款，工单['.$info['name'].']')->AddOrderNoPayNotice();
            /*生成待交款信息*/
            (new AddOrderMoneyRecord())->setOrderMoneyId($info['id'])->noPay();
            /*挂上未交款标签*/
            (new OrderBindingExtension($this->orderId,$this->opUid,2))->save();
        }
        /*通知*/
        (new OrderNotice($this->orderId,$this->opUid))->releaseTask();
        /*记入工单日志*/
        $this->writingOrderLog('STARTTACK','工单id为'.$this->orderId.'工单开始');
        return true;
    }

    /**
     * 催款
     * 生成申请记录
     */
    public function orderApplyUrgeMoney(){
        $model =  (new IsOrder($this->orderId));
        /*判断工单是否进行中*/
        $model->isExecuteOrder();
        /*判断是否存在未交款记录*/
        $moneyModel  =  (new GetOrderMoney($this->orderId,false));
        $moneyRecord =  $moneyModel->getNoPayRecord();
        /*不存在*/
        if(!is_numeric($moneyRecord['status'])){
            $noticeModel = new AddOrderNotice();
            /*生成交款通知记录*/
            $result  = $noticeModel->setOpUid($this->opUid)->setOrderId($this->orderId)
                ->setContent('催款操作')->AddOrderNoPayNotice();
            /*生成待交款信息*/
            (new AddOrderMoneyRecord())->setOrderMoneyId($moneyRecord['id'])->setNoticeId($result)->noPay();
            /*挂上未交款标签*/
            (new OrderBindingExtension($this->orderId,$this->opUid,2))->save();
        }
        /*发生通知*/

        /*记入工单日志*/
        $this->writingOrderLog('RELEASE','工单id为'.$this->orderId.'发布了任务');
    }

    /**
     * 同意催款通知
     */
    public function agreeOrderApplyUrgeMoney(){
        $model =  (new IsOrder($this->orderId));
        /*判断工单是否进行中*/
        $model->isExecuteOrder();
        /*判断是否存在未交款记录*/
        $moneyModel  =  (new GetOrderMoney($this->orderId,false));
        $moneyRecord =  $moneyModel->getNoPayRecord();
        /*该工单已不存在未交款记录*/
        if(empty($moneyRecord)){
            exception('该工单不存在未交款记录');
        }
        $noticeModel = new AddOrderNotice();
        /*生成交款通知记录*/
        $result  = $noticeModel->setOpUid($this->opUid)->setOrderId($this->orderId)
            ->setContent('催款操作')->AddOrderNoPayNotice();
        /*未生成交款记录*/
        if($moneyRecord['status'] == null){
            /*生成待交款信息*/
            (new AddOrderMoneyRecord())->setOrderMoneyId($moneyRecord['id'])->noPay();
            /*挂上未交款标签*/
            (new OrderBindingExtension($this->orderId,$this->opUid,2))->save();
        }
        /*发生通知*/

        /*记入工单日志*/
        $this->writingOrderLog('RELEASE','工单id为'.$this->orderId.'发布了任务');
    }
    /**
     * 工单交款回调
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderPayMoney(){
        /*获取交款期数id*/
        $info =  (new GetOrderMoney($this->orderId,false))->setMoneyId()->getFindInfo();
        /*已经交款*/
        if($info['status'] == 1){
            exception('该期已经交款,请勿重复交款');
        }
        /*查询是否生成记录 未生成记录*/
        if(!is_numeric($info['status'])){
            $noticeModel = new AddOrderNotice();
            /*生成交款通知记录*/
            $result  = $noticeModel->setOpUid($this->opUid)->setOrderId($this->orderId)
                ->setContent('主动交款记录')->AddOrderNoPayNotice();
            /*生成支付信息*/
            (new AddOrderMoneyRecord())->setOrderMoneyId($info['id'])
                ->setNoticeId($result)->setPayType()->setPayUserId()->pay();
        }
        /*如果记录存在 改变状态*/
        if($info['status'] == 0){
            (new EditOrderMoneyRecord())->setOrderMoneyId($info['id'])->setNoticeId($result)->pay();
        }
        /*查询是否挂有代缴款标签*/
        /*如果有去掉标签*/
    }

    /**
     * 工单催签署
     * 生成交款记录
     * 工单挂上待签收标签
     * 通知用户
     * 通知设计师
     */
    public function OrderUrgeSign(){

    }

    /**
     * 工单签收
     * 改变签收记录状态
     * 工单取消待签收标签
     * 通知设计师
     * 通知监理
     */
    public function OrderSign(){

    }


    protected function writingOrderLog($type,$content){
        $log =  $this->getOrderLogModel();
        $log->setOptionType($type);
        $log->setTaskId($this->taskOrderId);
        $log->setOptionUserId($this->opUid);
        $log->setSubmitParameter(json_encode($this->data));
        $log->setOptionContent($content);
        $log->save();
        return true;
    }
}